package com.bootdo.program.weixin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.config.Constant;
import com.bootdo.common.controller.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.program.weixin.domain.WxAdvertDO;
import com.bootdo.program.weixin.service.WxAdvertService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 微信广告表
 * 
 * @author guoyunfei
 * @email 2549789059@qq.com
 * @date 2021-08-03 21:20:52
 */
 
@Controller
@RequestMapping("/wallpaper/wxAdvert")
public class WxAdvertController extends BaseController {
	@Autowired
	private WxAdvertService wxAdvertService;

	@GetMapping()
	@RequiresPermissions("wallpaper:wxAdvert:wxAdvert")
	String WxAdvert(){
	    return "wallpaper/wxAdvert/wxAdvert";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wallpaper:wxAdvert:wxAdvert")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<WxAdvertDO> wxAdvertList = wxAdvertService.list(query);
		int total = wxAdvertService.count(query);
		PageUtils pageUtils = new PageUtils(wxAdvertList, total);
		return pageUtils;
	}

	@GetMapping("form/{id}")
	@RequiresPermissions("wallpaper:wxAdvert:wxAdvert")
	String WxAdvert(@PathVariable Integer id,Model model){
		model.addAttribute("id",id);
		return "wallpaper/wxAdvert/wxAdvertForm";
	}

	@GetMapping("/get/{id}")
	@ResponseBody
	@RequiresPermissions("wallpaper:wxAdvert:wxAdvert")
	R get (@PathVariable Integer id){
		WxAdvertDO wxAdvert = wxAdvertService.get(id);
		Map<String,Object> map = new HashMap<>();
		map.put("wxAdvert",wxAdvert);
		return R.ok(map);
	}
	

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wallpaper:wxAdvert:wxAdvert")
	public R update( WxAdvertDO wxAdvert){

		WxAdvertDO wxAdvertDO = wxAdvertService.getByAppid(wxAdvert.getAppid());
		if(null == wxAdvert.getId()){
			if(null != wxAdvertDO){
				return R.error("appid不可重复");
			}
			wxAdvertService.save(wxAdvert);
			return R.ok();
		}
		wxAdvertService.update(wxAdvert);
		return R.ok();
	}


	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wallpaper:wxAdvert:wxAdvert")
	public R remove( Integer id){
		if(wxAdvertService.remove(id)>0){
			return R.ok();
		}
		return R.error();
	}


}
